import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;

import javax.sound.midi.Patch;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import lejos.pc.comm.NXTConnector;



public class finalBHost extends JFrame {
	static finalBHost st;
	
	static InputStream ins;
	static DataInputStream instr;
	static OutputStream outs;
	static DataOutputStream outstr;

	static int current_x=6;
	static int current_y=6;
	
	static int orientation=3; // haritaya gore 3 sag, 9 sol, 12 yukari, 6 asagi
	static int nxtDirection=0; // 3 duz; 12 sol; 6 sag; 9 geri; 0 hicbirsey
	static Point matrix[][];
	
	public static final int CANVAS_WIDTH  = 390;
	public static final int CANVAS_HEIGHT = 390;	
	private static DrawCanvas canvas;

	private static class wall {
		final int wall_x1, wall_y1, wall_x2, wall_y2;
	
	    public wall(int x1, int y1, int x2, int y2) {
	        this.wall_x1 = x1;	this.wall_y1 = y1;
	        this.wall_x2 = x2;  this.wall_y2 = y2;
	    }  
	}

	private static class patch {
		final int patch_x1, patch_y1, patch_x2, patch_y2;
	
	    public patch(int x1, int y1, int x2, int y2) {
	        this.patch_x1 = x1;	this.patch_y1 = y1;
	        this.patch_x2 = x2;  this.patch_y2 = y2;
	    }  
	}
	
	private final LinkedList<wall> walls = new LinkedList<wall>();
	private final LinkedList<patch> patches = new LinkedList<patch>();
	
	
	public void addWall(int x1, int x2, int x3, int x4) {
	    walls.add(new wall(x1,x2,x3,x4));        
	//    repaint();
	}
	
	public void addPatch(int x1, int x2, int x3, int x4) {
	    patches.add(new patch(x1,x2,x3,x4));        
	//    repaint();
	}

	private static class Point {
		final int x, y;
		boolean visited=false;
		boolean inMap = false;
		
	    public Point(int x1, int y1) {
	        this.x = x1;	this.y = y1;
	    }  
	}
	
	public static int counter = 240;
	
	public finalBHost() {
		  
		  canvas = new DrawCanvas();    // Construct the drawing canvas
	      canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
	 
	      // Set the Drawing JPanel as the JFrame's content-pane
	      Container cp = getContentPane();
	      cp.add(canvas);
	       
	      this.setDefaultCloseOperation(EXIT_ON_CLOSE);   // Handle the CLOSE button
	      this.pack();              // Either pack() the components; or setSize()
	      this.setTitle("Map of The Robot");  // this JFrame sets the title
	      this.setVisible(true);    // this JFrame show
	      
	
	      matrix = new Point[12][12];
	      for (int i = 0; i < 12; i++) {
				for (int j = 0; j < 12; j++) {
					matrix[i][j] = new Point(i, j);
				}
	      }
	}	

	public static void main(String[] args) throws IOException {
				
		st = new finalBHost();
		st.initialization();
		
	}// end of main
	
	public void initialization() throws IOException {
		
		NXTConnector conn = new NXTConnector();
		boolean connected = conn.connectTo("btspp://group5" );
		
		// Connect to any NXT over Bluetooth
		if ( connected ) {
		    System.out.println("Connected!");
		} else {
		    System.out.println("Could not connect to any NXTs");
		    System.exit(1);
		}
			
		while(true) {
		
			ins = conn.getInputStream();
			instr = new DataInputStream( ins );
			outs = conn.getOutputStream();
			outstr = new DataOutputStream( outs );
				
			int x_coor = instr.readInt();
			int y_coor = instr.readInt();
			boolean front_distance = instr.readBoolean();
			boolean right_distance = instr.readBoolean();
			boolean back_distance = instr.readBoolean();
			boolean left_distance = instr.readBoolean();
			boolean colour = instr.readBoolean();
			
			System.out.println("Input Values :");
			System.out.println("front : "+front_distance+" right : "+right_distance+" left "+left_distance+" back : "+back_distance);
			System.out.println("Coordinate x :"+x_coor+" y : "+y_coor);
			
			addwallonMap(x_coor,y_coor,orientation, front_distance,right_distance,left_distance,back_distance);
			addpatchonMap(x_coor,y_coor,colour);		
					
			canvas.repaint();
			}
		
		
	}

	public void addpatchonMap (int x, int y, boolean  colour) {
		
		if(colour == true) {	
			addPatch(30*(x),30*(12-y),30*(x),30*(12-y)+1);
		}
	}
	
	public void addwallonMap (int x, int y, int orientation, boolean  front, boolean  right, boolean  left, boolean  back) {
		
		if(front == true) {	
			addWall(30*(x),30*(12-y),30*(x)+30,30*(12-y));
		}
		if(right == true) {	
			addWall(30*((x)+1),30*((12-y)+1),30*((x)+1),30*((12-y)+1)-30);
		}
		if(left == true) {	
			addWall(30*(x),30*((12-y)+1),30*(x),30*((12-y)+1)-30);
		}
		if(back == true) {	
			addWall(30*(x),30*((12-y)+1),30*(x)+30,30*((12-y)+1));
		}
		
		
	}
	
	private class DrawCanvas extends JPanel {
	      // Override paintComponent to perform your own painting
	      @Override
	      public void paintComponent(Graphics g) {
	         super.paintComponent(g);     // paint parent's background
	         setBackground(Color.WHITE);  // set background color for this JPanel
	 
	         
	         for(int i=1; i<13; i++) {
	        	 g.setColor(Color.LIGHT_GRAY);
	        	 g.drawLine(30*i, 0, 30*i, CANVAS_HEIGHT);
	        	 g.drawLine(0, 30*i, CANVAS_WIDTH, 30*i);
	        	 
	        	 g.setColor(Color.BLUE);
	        	 g.drawString(Integer.toString(i), 12+i*30, 20);
	        	 g.drawString(Integer.toString(i), 10, 20+i*30);
	         }
	         
	         g.setColor(Color.ORANGE);
	         g.fillRect(180+1, 180+1, 29, 29);
	         
	         g.setColor(Color.BLACK);
	         Graphics2D g2 = (Graphics2D) g;
	         g2.setStroke(new BasicStroke(2));

	         for (wall w : walls) {
	             g.drawLine(w.wall_x1, w.wall_y1, w.wall_x2, w.wall_y2);
	         }
	         
	         for (patch p : patches) {
	             //g.drawRect(p.patch_x1, p.patch_y1, p.patch_x2, p.patch_y2);
	             g.fillRect(p.patch_x1, p.patch_y1, 30, 30);
	             System.out.println("Ciz");
	         }
	         
	      }
	}


	
}
